const WebSocket = require('ws');

const wss = new WebSocket.Server({port: 8080});
//
// function generateTrayData(startingTrayNumber, totalTrays) {
//     const trayData = {};
//
//     // 模拟生成托盘的状态数据
//     for (let i = startingTrayNumber; i < startingTrayNumber + totalTrays; i++) {
//         const trayInGoodsData = Math.random() < 0.5; // 随机生成true或false
//         const trayErrStatusData = Math.random() < 0.2; // 随机生成true或false
//
//         trayData[i] = {
//             trayInGoodsData,
//             trayErrStatusData
//         };
//     }
//
//     return trayData;
// }
//
// wss.on('connection', function connection(ws) {
//     let startingTrayNumber = 1;
//     const totalTrays = 192; // 根据你的具体需求调整这个值
//
//     setInterval(() => {
//         const trayData = generateTrayData(startingTrayNumber, totalTrays);
//
//         ws.send(JSON.stringify(trayData));
//         console.log(JSON.stringify(trayData))
//
//         startingTrayNumber += 1;
//         if (startingTrayNumber > totalTrays) {
//             startingTrayNumber = 1;
//         }
//     }, 2000); // 每隔2秒推送一条消息
// });
//
// console.log('WebSocket server started on port 8080');

function generateTrayData(originTrayCode, totalTrays) {
    const trayMsg = {
        trayInGoods: [],
        trayErrStatus: [],
    };

    // 模拟生成托盘的状态数据
    for (let i = originTrayCode - 1; i < totalTrays + originTrayCode - 1; i++) {
        const trayInGoodsData = Math.random() < 0.5; // 随机生成true或false
        const trayErrStatusData = Math.random() < 0.2; // 随机生成true或false

        trayMsg.trayInGoods.push(trayInGoodsData);
        trayMsg.trayErrStatus.push(trayErrStatusData);
    }

    return trayMsg;
}

wss.on('connection', function connection(ws) {
    const totalTrays = 192; // 根据你的具体需求调整这个值
    const originTrayCode = 150;

    setInterval(() => {
        const trayData = generateTrayData(originTrayCode, totalTrays);

        const dataToSend = {
            equipmentMsg: {
                originTrayCode: originTrayCode,
            },  // 你可以在这里填充你需要的设备信息
            trayMsg: trayData,
            ScanEquipmentMsg: {
                scanStatus: Math.random() < 0.5, // 模拟生成扫描设备的状态
            },
            CommunicationMsg: {
                isOnline: Math.random() < 0.5, // 模拟生成通信设备的在线状态
            },
        };

        ws.send(JSON.stringify(dataToSend));
        console.log(JSON.stringify(dataToSend))

    }, 1000); // 每隔2秒推送一条消息
});

console.log('WebSocket server started on port 8080');
